package com.zmj.mobile.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.zmj.framework.response.Result;
import com.zmj.mobile.entity.ActHiProcinst;
import com.zmj.mobile.entity.ActHiTaskinst;
import com.zmj.mobile.entity.UserEntity;
import com.zmj.mobile.service.IActHiProcinstService;
import com.zmj.mobile.service.IActHiTaskinstService;
import com.zmj.mobile.service.IUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@Api(tags = {"ActController"})
@RequestMapping("/v1/api/act/ext")
public class ActController {

    @Autowired
    private IActHiProcinstService hiProcinstService;

    @Autowired
    private IActHiTaskinstService hiTaskinstService;

    @Autowired
    private IUserService userService;

    @GetMapping("/importAct")
    public Result<String> importAct() {
        Map<String, String> userMap = userService
                .list().stream().collect(Collectors.toMap(UserEntity::getRealName, UserEntity::getUsername, (r1, r2) -> r2));
        ExcelReader reader = ExcelUtil.getReader("C:\\Users\\acton\\Desktop\\研发试制、采购申请流程(全部)20240202145938.xlsx");
        reader.setSheet(2);
        int start = 2;
        int end = 1000;
        int head = 1;
        String procDefId = "研发试制、采购申请流程";
        List<Map<String, Object>> read = reader.read(head, start, end);
        // actHiProcinst.setAssignee(c.get("经办人").toString());
        List<ActHiProcinst> actHiProcinsts = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : read) {
            if ("已撤回".equals(stringObjectMap.get("申请状态").toString())
                    || stringObjectMap.get("标题").toString().contains("撤销")) {
                continue;
            }
            ActHiProcinst actHiProcinst = new ActHiProcinst();
            String no = stringObjectMap.get("申请编号").toString();
            actHiProcinst.setId(no);
            actHiProcinst.setBusinessKey(no);
            actHiProcinst.setRev(1);
            actHiProcinst.setProcInstId(no);
            actHiProcinst.setStartTime(DateUtil.parse(stringObjectMap.get("发起时间").toString()));
            actHiProcinst.setEndTime(DateUtil.parse(stringObjectMap.get("完成时间").toString()));
            actHiProcinst.setStartUserId(stringObjectMap.get("发起人工号").toString());
            actHiProcinst.setProcDefId(procDefId);
            actHiProcinst.setName(stringObjectMap.get("标题").toString());
            actHiProcinst.setActHiTaskinsts(stringObjectMap.get("审批记录").toString());
            // actHiProcinst.setAssignee(c.get("经办人").toString());
            actHiProcinst.setCategory(stringObjectMap.get("申请状态").toString());
            actHiProcinsts.add(actHiProcinst);
        }
        hiProcinstService.saveBatch(actHiProcinsts);
        List<ActHiTaskinst> actHiTaskinsts = new ArrayList<>();
        for (ActHiProcinst actHiProcinst : actHiProcinsts) {
            String actHiTaskinsts1 = actHiProcinst.getActHiTaskinsts();
            String[] split = actHiTaskinsts1.split(";");
            for (String s : split) {
                String[] split1 = s.split("\\|");
                ActHiTaskinst actHiTaskinst = new ActHiTaskinst();
                actHiTaskinst.setRev(1);
                actHiTaskinst.setProcDefId(procDefId);
                actHiTaskinst.setTaskDefId(UUID.randomUUID().toString());
                actHiTaskinst.setProcInstId(actHiProcinst.getProcInstId());
                actHiTaskinst.setName(actHiProcinst.getName());
                actHiTaskinst.setAssignee(userMap.get(split1[0]));
                actHiTaskinst.setEndTime(DateUtil.parse(split1[split1.length - 1]));
                actHiTaskinst.setStartTime(DateUtil.parse(split1[split1.length - 1]));
                if (split1.length == 4) {
                    actHiTaskinst.setDeleteReason(split1[2]);
                }
                actHiTaskinst.setPriority(50);
                actHiTaskinst.setCategory(actHiProcinst.getCategory());
                actHiTaskinst.setLastUpdatedTime(actHiProcinst.getEndTime());
                actHiTaskinsts.add(actHiTaskinst);
            }

        }
        hiTaskinstService.saveBatch(actHiTaskinsts);
        return Result.success();
    }
}
